package com.caifu.controller;

import com.caifu.bean.Result;
import com.caifu.bean.ResultUtil;
import com.caifu.service.UtilService;
import com.caifu.util.Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Auther: Lyf
 * @Date: 2020/11/3 15:11
 * @Description: 公共组件
 */
@Controller
@RequestMapping("/common")
public class CommonController extends AbstractController{

    @Autowired
    private UtilService utilService;

    @GetMapping( "/index")
    public String jumpUserInfoList() {
        return "view/common/selectUser";
    }

    @PostMapping("/getUserList")
    @ResponseBody
    public Result getUserList(int page, int pageSize,String userName, String orgNo,String userPid){
        try{
            int start = (page - 1) * pageSize;
            List<String> orgList = new ArrayList<>();
            if(StringUtils.isNotBlank(orgNo)) {
                List<Map<String,String>> orgs = utilService.getJurisdictionByParentNo(Integer.parseInt(orgNo));
                if(orgs != null && orgs.size() > 0){
                    for(Map<String,String> m : orgs){
                        orgList.add(String.valueOf(m.get("org_id")));
                    }
                }
            }/*else{
                orgList = utilService.getJurisdiction(getUserNo());
            }*/
            List<Map<String,String>> list = utilService.getUserList(start,pageSize,userName,userPid,orgList);
            int count = utilService.getUserListCount(userName,userPid,orgList);
            return new Result(0,"获取用户列表成功！",count,list);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(-1,"获取用户列表失败！");
        }
    }

    public static void main(String[] args) {
        try{
            List<String> list = Util.getMonthFullDay(2020,11,"dd");
            if(list != null && list.size() > 0){
                for(String s : list) {
                    System.out.println(s);
                }
            }
            System.out.println("==========================================华丽的分割线==========================================");
            System.out.println(Util.getDateWeek(null,-1));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
